﻿Imports VSModel
Imports System.Linq
Imports System.Data
Imports Microsoft.Data.Extensions
Imports System.Collections.Generic
Imports System.Text
Imports System.Data.Common
Imports System.Data.SqlClient
Imports Cls_Common


Partial Class Control_WhatCookToDay_Index
    Inherits System.Web.UI.UserControl
    Dim iMenuId As Integer = 0
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("MenuId") Is Nothing Then
                iMenuId = Page.RouteData.Values("MenuId")
                LoadItemIndex(iMenuId)
            End If
        End If
    End Sub

    Sub LoadItemIndex(ByVal iMenuid As Integer)
        Dim cls As New clsDA_HomeIndex
        Dim dtsSrc As DataSet = cls.Select_Content_Home_WhatCookToday()

        If Not dtsSrc Is Nothing AndAlso dtsSrc.Tables.Count > 0 Then
            If dtsSrc.Tables.Count > 1 Then
                ViewState("vContentMenu") = dtsSrc.Tables(1)
            Else
                ViewState("vContentMenu") = Nothing
            End If
        End If

        If Not dtsSrc.Tables(0) Is Nothing Then
            With rptNewsHome
                .DataSource = dtsSrc.Tables(0)
                .DataBind()
            End With
        End If
        ViewState("vContentMenu") = Nothing

        cls.Destructor()
        cls = Nothing

        ''Load data knor Coorner
        'Using dta As New VSEntities
        '    Dim sm = (From m In dta.uspSelectSubMenu(idKnorCooner) Select m.Menuid).ToArray
        '    Dim kn = (From c In dta.Contents Where c.StatusId = 3 And (c.MenuId = idKnorCooner Or sm.Contains(c.MenuId)) And Now >= c.PublishedDate And (c.eEffectiveDate >= Now Or c.eEffectiveDate Is Nothing) Order By c.PublishedDate Descending Select c).Skip(0).Take(1).ToList
        '    With rptKnorCooner
        '        .DataSource = kn
        '        .DataBind()
        '    End With
        'End Using
    End Sub

    'Bind du lieu len rptNewsHome
    Protected Sub rptNewsHome_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptNewsHome.ItemDataBound
        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)


        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & row("QueryString") & "-" & iMenuId & "-" & row("Id") & "f.html"
        hplTitle.Text = row("Title")

        'Load content of Formula
        Dim rptNews As Repeater = CType(e.Item.FindControl("rptNews"), Repeater)
        Dim dtContentMenu As DataTable = CType(ViewState("vContentMenu"), DataTable)
        Dim dvContentMenu As New DataView
        dvContentMenu = dtContentMenu.DefaultView

        'take first row
        dvContentMenu.RowFilter = "FormulaTypeId=" & row("ID")
        'take first row
        If Not dvContentMenu Is Nothing AndAlso dvContentMenu.Count > 0 Then
            Dim dtFirstContent As DataTable = dtContentMenu.Clone
            Dim dr As DataRow = dtFirstContent.NewRow
            dr("Id") = dvContentMenu(0).Row("Id").ToString
            dr("Title") = dvContentMenu(0).Row("Title").ToString
            dr("Description") = dvContentMenu(0).Row("Description").ToString
            dr("Image") = dvContentMenu(0).Row("Image").ToString
            dr("QueryString") = dvContentMenu(0).Row("QueryString").ToString
            dr("PublishedDate") = dvContentMenu(0).Row("PublishedDate")
            dr("FormulaTypeId") = dvContentMenu(0).Row("FormulaTypeId")
            dr("QueryStringFT") = dvContentMenu(0).Row("QueryStringFT").ToString
            dtFirstContent.Rows.Add(dr)
            dtFirstContent.AcceptChanges()
            rptNews.DataSource = dtFirstContent
            rptNews.DataBind()
        End If
    End Sub

    'rptNews_ItemDataBound
    Protected Sub rptNews_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        hplImg.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row("QueryStringFT") & "/" & row("QueryString") & "-" & row("Id") & ".html"
        hplImg.Text = "<img src=""" & row("Image").ToString & """ class=""imgLage_left"" />"

        'hplNewsTitle
        Dim hplNewsTitle As HyperLink = CType(e.Item.FindControl("hplNewsTitle"), HyperLink)
        hplNewsTitle.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row("QueryStringFT") & "/" & row("QueryString") & "-" & row("Id") & ".html"
        hplNewsTitle.Text = row("Title")

        Dim hplNewsDesc As HyperLink = CType(e.Item.FindControl("hplNewsDesc"), HyperLink)
        hplNewsDesc.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row("QueryStringFT") & "/" & row("QueryString") & "-" & row("Id") & ".html"
        hplNewsDesc.Text = row("Description")

        Dim rptNewsRelated As Repeater = CType(e.Item.FindControl("rptNewsRelated"), Repeater)

        'Load content of menu
        Dim dtContentMenu As DataTable = CType(ViewState("vContentMenu"), DataTable)
        Dim dvContentMenu As New DataView
        dvContentMenu = dtContentMenu.DefaultView

        'take three row
        dvContentMenu.RowFilter = "FormulaTypeId=" & row("FormulaTypeId") & " and Id <> " & row("Id")
        rptNewsRelated.DataSource = dvContentMenu
        rptNewsRelated.DataBind()

    End Sub
    'rptNewsRelated_ItemDataBound
    Protected Sub rptNewsRelated_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim hplImgNewsRelated As HyperLink = CType(e.Item.FindControl("hplImgNewsRelated"), HyperLink)
        hplImgNewsRelated.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row("QueryStringFT") & "/" & row("QueryString") & "-" & row("Id") & ".html"
        hplImgNewsRelated.Text = "<img src='" & row("Image").ToString & "' />" '<p>" & row("Title").ToString & "</p>"

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = MainDomain & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & row("QueryStringFT") & "/" & row("QueryString") & "-" & row("Id") & ".html"
        Dim lirTitle As Literal = CType(e.Item.FindControl("lirTitle"), Literal)
        lirTitle.Text = row("Title").ToString

    End Sub


End Class
